package vcp.server.utility;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import vcp.entity.Customer;
import vcp.entity.Subscription;
/**
 * 
 * @author VCP team
 * <p>
 * Communication Provider class sends mail ans SMS
 * </p>
 * 
 */
public class CommunicationProvider
{

	private static final String VCP_SYSTEM_TITLE = "VCP Inc. - Auto Reminder - Do not replay this msg";
	private static final String VCP_SYSTEM_EMAIL = "vcpnoreplay@gmail.com";
	private static final String VCP_SYSTEM_PASSWORD = "rs2410ar";
	/**
	 * references properties
	 */
	private Properties emailProperties;
	/**
	 * references Authenticator
	 */
	private Authenticator authenticator;
	/**
	 *  class constructor  initialize CommunicationProvider
	 */
	public CommunicationProvider ()
	{
		this.initAuthenticator();
		this.initEmailProperties();
	}
	/**
	 *  Email send method
	 */
	public synchronized void sendEmail ( Subscription subscription, Customer customer, String emailBody )
	{
		Session session = Session.getDefaultInstance(this.emailProperties, this.authenticator);
		try
		{
			String to = customer.getUserEmail();
			Message message = new MimeMessage(session);

			message.setFrom(new InternetAddress(CommunicationProvider.VCP_SYSTEM_EMAIL));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

			message.setSubject(CommunicationProvider.VCP_SYSTEM_TITLE);
			message.setText(emailBody);

			Transport.send(message);

			System.out.println("Email was send to:" + customer.getUserFirstName() + " to email address:" + customer.getUserEmail());

		}
		catch ( MessagingException e )
		{
			e.printStackTrace();
		}
	}
	/**
	 *  SMS send
	 */
	// private void sendSMS ( Subscription subscription, Customer customer )
	// {
	// try {
	// String recipient = "06205555555";
	// String message = "Hello World";
	// String username = "admin";
	// String password = "abc123";
	// String originator = "06201234567";
	//
	// String requestUrl = "http://127.0.0.1:9501/api?action=sendmessage&" +
	// "username=" + URLEncoder.encode(username, "UTF-8") +
	// "&password=" + URLEncoder.encode(password, "UTF-8") +
	// "&recipient=" + URLEncoder.encode(recipient, "UTF-8") +
	// "&messagetype=SMS:TEXT" +
	// "&messagedata=" + URLEncoder.encode(message, "UTF-8") +
	// "&originator=" + URLEncoder.encode(originator, "UTF-8") +
	// "&serviceprovider=GSMModem1" +
	// "&responseformat=html";
	//
	//
	//
	// URL url = new URL(requestUrl);
	// HttpURLConnection uc = (HttpURLConnection)url.openConnection();
	//
	// System.out.println(uc.getResponseMessage());
	//
	// uc.disconnect();
	//
	// } catch(Exception ex) {
	// System.out.println(ex.getMessage());
	//
	// }
	// }
	// }
	/**
	 *  initialize Authenticator
	 */
	private void initAuthenticator ()
	{
		this.authenticator = new Authenticator()
		{
			@Override
			protected PasswordAuthentication getPasswordAuthentication ()
			{
				return new PasswordAuthentication(CommunicationProvider.VCP_SYSTEM_EMAIL, CommunicationProvider.VCP_SYSTEM_PASSWORD);
			}
		};
	}
	/**
	 *  initialize email properties
	 */
	private void initEmailProperties ()
	{
		this.emailProperties = new Properties();
		this.emailProperties.put("mail.smtp.host", "smtp.gmail.com");
		this.emailProperties.put("mail.smtp.socketFactory.port", "465");
		this.emailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		this.emailProperties.put("mail.smtp.auth", "true");
		this.emailProperties.put("mail.smtp.port", "465");
	}
}
